package br3akr.DeadlyDinnerParty.Server.Actions;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import br3akr.DeadlyDinnerParty.Server.Engine.Session;
import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;
import br3akr.DeadlyDinnerParty.Server.World.Area;
import br3akr.DeadlyDinnerParty.Server.World.GameCharacter;

public class StowAction extends Action {
	boolean actionComplete;
    private ActionResponse actionResponse;
    public static final Set<String> keywords;
	static
	{
		Set<String>tempKeywords = new HashSet<String>();
		tempKeywords.add("Stow");
		keywords = Collections.unmodifiableSet(tempKeywords);
	}
    
    public StowAction() {
        super("Stow", false);
    }
    
    public ActionResponse doAction(String action, Session session) throws DeadlyDinnerPartyException {
		if (action == null) {
			throw new DeadlyDinnerPartyException ("No action found.");
		}
		if (session == null) {
			throw new DeadlyDinnerPartyException ("No session found.");
		}

		GameCharacter actor = session.getCharacter();
		
		if (actor == null) {
			throw new DeadlyDinnerPartyException ("No character found for your session.");
		}
    	
    	actionResponse = new ActionResponse();
    	actionResponse.setActingCharacter(session.getCharacter());
    	Area location = session.getCharacter().getLocation();
    	Set<GameCharacter> localOnlookers = location.getAreaOnlookers(session.getCharacter(), null);
    	actionResponse.addAffectedArea(location);

    	if (actor.getHeldItem() == null) {
    		throw new DeadlyDinnerPartyException("You're not holding anything.");
    	}
    	
		
		actionResponse.addOnlookers(localOnlookers, actor.getName() + " puts " + actor.getHeldItem().getName() + " in their inventory.");
		actionResponse.setActingCharacterDescription("You put " + actor.getHeldItem().getName() + " in your inventory.");
		
		actor.addItem(actor.getHeldItem());
		actor.holdItem(null);
		
		beginRecovery(session.getCharacter(), 3000);
    	actionComplete = true;
        return actionResponse;
    }
	
    public ActionResponse getActionResponse() {
		return actionResponse;
	}
    
    public Set<String> getKeywords() {
        return keywords;
    }
}